<template>
<div class="layerRtb layerRtb-threecolumn layerRtb-right">
    <three-title :title="{name:name}" :close="false">
        <span tag="i" class="rig_close fr" @click="$parent.$parent.clickFiveShow()"></span>
    </three-title>
     <div class="plr10">
        <div class="analyItem">
            <p class="analyItemTit tx-center">交底时间</p>
            <div class="analyItemCon">
                <p class="col-md-12">
                    <span class="cLightGray pr8 fl">时间</span>
                    <el-date-picker
                        :picker-options="pickerBeginDateBefore"
                        class="fl"
                        v-model="time"
                        type="date"
                        placeholder="选择日期">
                    </el-date-picker>
                </p>
            </div>
        </div>
    </div>
    <div class="layerRtb-scroll thinScroll" v-scrollHeight = "18">
        <div class="analyItem" v-for="(item, index) in safeCloseData" :key="index">
            <p class="analyItemTit tx-center">{{item.majorJobName}}</p>
            <div class="analyItemCon">
                <div class='flex'>
                    <div class="clearfix col-md-11">
                        <p class="col-md-4">
                            <span class="cLightGray pr8">人员姓名</span>
                            <span>{{item.workerName}}</span>
                        </p>
                        <p class="col-md-4">
                            <span class="cLightGray pr8">电话</span>
                            <span>{{item.mobile}}</span>
                        </p>
                        <p class="col-md-3" v-if="item.description !== null">
                            <span class="cLightGray pr8">级别</span>
                            <span>{{item.majorJobName}}级</span>
                        </p>
                    </div>
                    <el-checkbox v-model="checkID[item.workerId]"></el-checkbox>
                </div>
            </div>
        </div>
    </div>
    <div class="layerRtb-footer">
        <div class="analyItem">
            <p class="analyItemTit tx-center">综合</p>
            <div class="analyItemCon">
                <p class="col-md-4">
                    <span class="cLightGray pr8">总人数</span>
                    <span>{{safeCloseData.length}}</span>
                </p>
                <div class="tx-center">
                    <input type="button" class="uiBtn-normal uiBtn-blue" value="确定" @click="submitButton()" v-if="buttonIsShow">
                </div>
            </div>
        </div>
    </div>
</div>
</template>
<script>
import { mapGetters, mapMutations } from 'vuex'
import { saveSafetyConfide, getSafetyConfide, getProjectPersonnelByOrderNo } from '../../../../Resources/Api/scene'
export default {
    props: ['titleName', 'url', 'type'],
    data () {
        return {
            pickerBeginDateBefore: {
                disabledDate: (time) => {
                    return time.getTime() < Date.now() - 8.64e7// 如果没有后面的-8.64e7就是不可以选择今天的
                }
            },
            checkID: [], // 复选框数组
            safeCloseData: null, // 现场交底数据
            personList: [], // 人员数据
            time: '', // 交底时间
            name: '',
            orderTypeId: '', // 订单id
            buttonIsShow: false // 按钮是否显示
        }
    },
    computed: {
        ...mapGetters(['updatePlate', 'twoToThreeInfo', 'userInfo', 'leftInfo'])
    },
    created () {
        this.name = this.titleName
        this.orderTypeId = this.$route.query.project.orderTypeId
        this.buttonType()
        this.getProjectPersonnelByOrderNo()
    },
    methods: {
        buttonType () {
            if (this.twoToThreeInfo.status === this.type) {
                this.buttonIsShow = true
            } else {
                const userLevel = this.userInfo.as_level
                if (userLevel.length > 0) {
                    userLevel.forEach(ele => {
                        if (Number(ele) === 82 || Number(ele) === 83) {
                            if (this.twoToThreeInfo.status === this.type) {
                                this.buttonIsShow = true
                            } else {
                                this.buttonIsShow = false
                            }
                        } else if (Number(ele) === 47) {
                            if (Number(this.userInfo.as_cardNo) === Number(this.leftInfo.supervisor_userno)) {
                                if (this.twoToThreeInfo.status === this.type) {
                                    this.buttonIsShow = true
                                } else {
                                    this.buttonIsShow = false
                                }
                            } else {
                                this.buttonIsShow = false
                            }
                        } else {
                            this.buttonIsShow = false
                        }
                    })
                } else {
                    this.buttonIsShow = false
                }
            }
        },
        ...mapMutations({
            setUpDatePlate: `SET_UPDATE_PLATE`
        }),
        // 获取人员
        getProjectPersonnelByOrderNo () {
            let parms = {
                orderNo: this.leftInfo.orderno
            }
            getProjectPersonnelByOrderNo(parms).then(results => {
                if (results.data.StatusCode === 1) {
                    const safeCloseData = results.data.Body
                    this.getSafetyConfide(safeCloseData)
                }
            }).catch(error => {
                console.log(error)
            })
        },
        // 获取交底数据
        getSafetyConfide (safeCloseData) {
            let parms = {
                orderCode: this.$route.query.code
            }
            getSafetyConfide(parms).then(results => {
                if (results.data.StatusCode === 1) {
                    if (results.data.Body.safetyConfideOrder !== null) {
                        this.time = results.data.Body.safetyConfideOrder.time !== null ? results.data.Body.safetyConfideOrder.time : ''
                        if (results.data.Body.safetyConfideOrder.personnel !== null) {
                            const data = results.data.Body.safetyConfideOrder.personnel.split(',')
                            data.forEach(element => {
                                const currentDate = safeCloseData.filter((item) => { return item.appCardNo === element })
                                if (currentDate && currentDate.length > 0) {
                                    this.checkID[currentDate[0].workerId] = true
                                }
                            })
                        }
                    }
                    this.safeCloseData = safeCloseData
                }
            }).catch(error => {
                console.log(error)
            })
        },
        // 提交数据
        submitButton () {
            if (this.time === '') {
                return false
            }
            const submitDataArray = []
            this.checkID.forEach((element, index) => {
                if (element) {
                    const currentDate = this.safeCloseData.filter((item) => { return item.workerId === Number(index) })
                    if (currentDate.length > 0) {
                        submitDataArray.push(currentDate[0].appCardNo)
                    }
                }
            })
            let parm = {
                orderNo: this.leftInfo.orderno,
                orderCode: this.$route.query.code,
                personnel: submitDataArray.length > 0 ? submitDataArray.join(',') : '',
                time: this.time,
                createUserNo: this.userInfo.as_cardNo,
                createUserName: this.userInfo.as_fullName
            }
            // 提交现场交底
            saveSafetyConfide({safetyConfide: parm}).then(results => {
                if (results.data.StatusCode === 1) {
                    this.setUpDatePlate()
                    this.$parent.$parent.clickFiveShow()
                }
            }).catch(error => {
                console.log(error)
            })
        }
    }
}
</script>
<style lang="">
    .fixedBox {
        display: flex;
        align-items: center;
    }
</style>
